// pages/phone/phone.js
import getIn from '../../sdk/utils/getIn';
import {showSuccess,showFail} from '../../utils/util';
import { API_GET_CODE, API_BIND_PHONE } from '../../sdk/business/url';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      sendBtn: '获取验证码',
      times: 60,
      un_send: true,
      param: {
        phone: '',
        code: '',
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
      const exdit = getIn(options, ['exdit']);
      const userInfo = wx.getStorageSync('userInfo');
      this.setData({ exdit: !!exdit, head_photo: userInfo.head_photo });
  },
  submit () {

  },
    // input回调函数
  bindReplaceInput(e) {
    const key = getIn(e.currentTarget.dataset, ['key']);
    const value = getIn(e.detail, ['value']);
    this.setData({ [`param.${key}`]: value })
  },
  // 发送验证码
  getCode() {
      const { un_send, times, param } = this.data;
      const phone = param.phone;
      const token = wx.getStorageSync('token');
      const reg = /^1(3|4|5|6|7|8)\d{9}$/;
      if (!phone || !reg.test(phone)) {
          showFail('请输入正确手机');
          return
      }
      if (un_send) {
          this.setData({
              un_send: false,
          }, () => {
              this.timeouts = setInterval(() => {
                  let time = this.data.times;
                  time--;
                  this.setData({
                      times: time,
                      sendBtn: `${time}s后重发`
                  })
                  if (time === 0) {
                      this.setData({
                          times,
                          sendBtn: '获取验证码',
                          un_send: true
                      })
                      clearInterval(this.timeouts);
                  }
              }, 1000)
          })
          const params = { phone, token}
          global.request(API_GET_CODE, params, (code, res) => {
            if(code == 'SUCCESS') {
                showSuccess('发送成功');
            } else if(code == 'FAIL') {
                showFail(res.msg);
            }
          })
      }
  },
  getUser(userInfo, callback) {
      getApp().wxLogin(userInfo, (code, res) => {
          if (code == 'SUCCESS') {
              callback();
          } else if(code == 'FAIL') {
              showFail(res.msg);
          }
      })
  },
  // 提交
  submit(e) {
    const token = wx.getStorageSync('token');
    const userInfo = wx.getStorageSync('userInfo');
    const bindPhone = (callback) => {
        const params = { ...this.data.param, bind_type:1, token };
        global.request(API_BIND_PHONE, params, (code, res) => {
            if(code == 'SUCCESS') {
                showSuccess('操作成功');
                callback();
            } else if(code == 'FAIL') {
                showFail(res.msg);
            }
        })
    }
    if (!userInfo.head_photo) {
        bindPhone(() => {
            this.getUser(e.detail, () => {
                global.navigateHelper.navigateBack();
            })
        })
    } else {
        bindPhone(() => {
            global.navigateHelper.navigateBack();
        })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
    if (this.timeouts) {
        clearInterval(this.timeouts);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {
  
  }
})